/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cs425proj.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author prateekpatil
 */
public class displayDiscusion extends HttpServlet {
private Connection con ;
    private Statement stm;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String getPara="";
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            
            
            int k =1;
            String click="";
            for(k=1;k<11;k++)
            {
             getPara = "dis"+k;
             System.out.println(getPara+":"+request.getParameter(getPara));
            if(request.getParameter(getPara)== null)
            {}
            else{
                click=request.getParameter(getPara);
                k=11; 
            }
                  }
            int dis_id=Integer.parseInt(click);
            String qry="select * from discussion where discussion_id='"+dis_id+"'";
                  this.startConnection();
                  stm=con.createStatement();
                  ResultSet rs1 = stm.executeQuery(qry);
                  int memid=0,movid=0,tid=0,vis=0;
                  String cont="",title="";
                  while(rs1.next())
                  {
                      memid=rs1.getInt(2);
                      cont=rs1.getString(3);
                      movid=rs1.getInt(4);
                      tid=rs1.getInt(5);
                      title =rs1.getString(6);
                      vis=rs1.getInt(7);
                      
                  }
                  
                  String qry1="select name from movie where movie_id='"+movid+"'";
                  String qry2="select theater_name from theater where theater_id='"+tid+"'";
                  
                  String qry3="select username from member where member_id='"+memid+"'"; 
                  String qry4="update discussion set visits=visits+1 where discussion_id='"+dis_id+"'"; 
                  String mname="";
                  String tname="";
                  String uname="";
                  
                  ResultSet rs2=stm.executeQuery(qry1);
                  while(rs2.next())
                  {
                    mname=rs2.getString(1);    
                  }
                  ResultSet rs3=stm.executeQuery(qry2);
                  while(rs3.next())
                  {
                    tname=rs3.getString(1);    
                  }

                  ResultSet rs4=stm.executeQuery(qry3);
                  while(rs4.next())
                  {
                    uname=rs4.getString(1);    
                  }

                  
                  stm.executeQuery(qry4);
                 
                request.setAttribute("mname", mname);
                request.setAttribute("memid", memid);
                request.setAttribute("uname", uname);
                request.setAttribute("cont", cont);
                request.setAttribute("tname", tname);
                request.setAttribute("title", title);
                request.setAttribute("vis", vis);
                request.setAttribute("dis_id", dis_id);
             String destination="/printDiscussion.jsp";
             RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
             rd.forward(request, response); 
                  
                  
            
            
            
            
            System.out.println("click:"+click);
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet displayDiscusion</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet displayDiscusion at : " +getPara+": "+click+  "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(displayDiscusion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(displayDiscusion.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
     public void startConnection(){
        
       String connStr ="jdbc:oracle:thin:@fourier.cs.iit.edu:1521:orcl";
       
       
       
           try{
               //load the drives
            Class.forName("oracle.jdbc.driver.OracleDriver");

            //initialize the connection variable
               con = DriverManager.getConnection(connStr, "rjairaj", "cs425dbo");
                     
                      
             }catch(SQLException e){e.printStackTrace();}
              catch(ClassNotFoundException cn){cn.printStackTrace();}
           
           }
    
    public void closeConnection(){
           try{
            con.commit();
            con.close();
    
           }catch(SQLException e){e.printStackTrace();}
    }
    
}
